//
//  HelloWorldScene.m
//  MessangerSkeleton
//
//  Created by User-16 on 4/1/14.
//  Copyright IOTrainingCamp 2014. All rights reserved.
//
// -----------------------------------------------------------------------

#import "GameScene.h"
#import "IntroScene.h"
#import "MessengerSprite.h"
#import "CCTiledMap.h"
#import "Configuration.h"
#import "CCDirector.h"
#import "TrackMap.h"
#import "GameObject.h"

#import "GamePlay.h"

@interface GameScene ()

@property  TrackMap *route;
@property  MessengerSprite *player;

@property CGPoint touchBegin;
@property CGPoint touchEnd;


@property GamePlay *gamePlay;
@end


@implementation GameScene

#pragma mark - Factory Method for the scene

+ (GameScene *)scene
{
    return [[self alloc] init];
}

- (id)init
{
    self = [super init];
    if (!self) return(nil);
    
    self.gamePlay = [GamePlay sharedInstance];
    
    // Enable touch handling on scene node
    self.userInteractionEnabled = YES;
    
    //setup the background
    self.route = [[TrackMap  alloc] init];
    
    
    [self addChild:self.route z:-1];
    
    
    NSLog(@"self.gamePlay.characterType = %d", self.gamePlay.characterType);
    // Add  the player
    self.player = [[MessengerSprite alloc] initWithType:self.gamePlay.characterType];
    self.player.position  = ccp(self.contentSize.width/4,self.contentSize.height/2 - 55);
    self.player.scale = 0.5;
    [self addChild:self.player];
    
    
    [self.player run];
    
    
    // Create a back button
    CCButton *backButton = [CCButton buttonWithTitle:@"[ Menu ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    backButton.positionType = CCPositionTypeNormalized;
    backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
    [backButton setTarget:self selector:@selector(onBackClicked:)];
    [self addChild:backButton];
    

    // Mute Button
    CCButton *muteButton = [CCButton buttonWithTitle:@"[ Mute ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    muteButton.positionType = CCPositionTypeNormalized;
    muteButton.position = ccp(0.15f,0.95f);
    [muteButton setTarget:[TransferDataThroughScenes sharedInstance] selector:@selector(muteSounds)];
    [self addChild:muteButton];
  
    
	return self;
}




// -----------------------------------------------------------------------
#pragma mark - Enter & Exit
// -----------------------------------------------------------------------

- (void)onEnter
{
    // always call super onEnter first
    [super onEnter];
    
    // In pre-v3, touch enable and scheduleUpdate was called here
    // In v3, touch is enabled by setting userInterActionEnabled for the individual nodes
    // Per frame update is automatically enabled, if update is overridden
    
}

// -----------------------------------------------------------------------

- (void)onExit
{
    // always call super onExit last
    [super onExit];
}

// -----------------------------------------------------------------------
#pragma mark - Touch Handler
// -----------------------------------------------------------------------

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    self.touchBegin = [touch locationInNode:self];
    
}
-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    self.touchEnd = [touch locationInNode:self];
    
    // computing the angle of the swipe
    float angle = ccpToAngle(ccpSub(self.touchBegin, self.touchEnd)) + M_PI;
    
    // take actions, according to the angle
    if (angle > M_PI + kBlindSpotForUserInteraction && angle < 2*M_PI - kBlindSpotForUserInteraction){
        [self.player slide];
    }
    if (angle > kBlindSpotForUserInteraction && angle < M_PI - kBlindSpotForUserInteraction){
        [self.player jump];
    }

}

// -----------------------------------------------------------------------
#pragma mark - Button Callbacks
// -----------------------------------------------------------------------

- (void)onBackClicked:(id)sender
{
    // back to intro scene with transition
    [[CCDirector sharedDirector] replaceScene:[IntroScene scene]
                               withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionRight duration:1.0f]];
}

-(void)update:(CCTime)dt {
    [self.route update:dt andSpeed:self.player.currentSpeed];
}

@end
